package com.wangjiyuan.giftshuo.ui.fragment.listtop.other;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.base.BaseFragment;
import com.wangjiyuan.giftshuo.bean.listtopbean.commentbean.CommentBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.everydaybean.EveryDayBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.maybelikebean.MayBeLikeBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.singleproductbean.SingleProductBean;

/**
 * A simple {@link Fragment} subclass.
 */
public class DetailsFragment extends BaseFragment implements ListTopContract.IListTopView{

    private WebView webView;
    private SingleProductBean.DataBean dataBean;
    private int id;
    private ListTopContract.IListTopPresenter iListTopPresenter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null){
            id = (int) bundle.getSerializable("itemId");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_details, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        webView = (WebView) view.findViewById(R.id.wv_details);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        startMVP();
    }

    private void startMVP() {
        iListTopPresenter = new ListTopPresenter(this);
        iListTopPresenter.getSingleProductData(id);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void initWebView() {

        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webView.loadUrl(url);
                return true;
            }
        });
        webView.loadUrl(dataBean.getUrl());
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showErrorInfo(String msg) {

    }

    @Override
    public void getEveryDayData(EveryDayBean everyDayBean) {

    }

    @Override
    public void getMayBeLikeData(MayBeLikeBean mayBeLikeBean) {

    }

    @Override
    public void getCommentData(CommentBean commentBean) {

    }

    @Override
    public void getSingleProductData(SingleProductBean singleProductBean) {
        dataBean = singleProductBean.getData();
        initWebView();
    }
}
